package com.yc.bran.snack.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yc.bran.snack.bean.CartInfo;
import com.yc.bran.snack.mapper.ICartInfoMapper;
import com.yc.bran.snack.service.ICartInfoService;

@Service
public class CartInfoServiceImpl implements ICartInfoService {

	@Autowired  
	private StringRedisTemplate redisTemplate;
	
	@Autowired  
	private ICartInfoMapper cartInfoMapper;

	private static final String CART_PREFIX = "uid:cart:";

	// 添加购物车
	@Override
	public void addToCart(Integer uid, Integer gid, Integer num, String gname, String gpic, Double gprice) {  
		String cartKey = CART_PREFIX + uid;  

		HashOperations<String, String, String> hashOps = redisTemplate.opsForHash();  

		ObjectMapper objectMapper = new ObjectMapper();  
		String cartInfoJson;  

		String existingCartInfoJson = hashOps.get(cartKey, gid.toString());  
		CartInfo cartInfo;  
		if (existingCartInfoJson != null) {  
			try {  
				cartInfo = objectMapper.readValue(existingCartInfoJson, CartInfo.class);  
				cartInfo.setNum(cartInfo.getNum() + num);   
			} catch (IOException e) {  
				throw new RuntimeException("Failed to deserialize CartInfo from JSON", e);  
			}  
		} else {  
			cartInfo = new CartInfo();  
			cartInfo.setUid(uid);  
			cartInfo.setGid(gid);  
			cartInfo.setNum(num);  
			cartInfo.setGname(gname);  
			cartInfo.setGpic(gpic);  
			cartInfo.setGprice(gprice);  
		}  

		try {  
			cartInfoJson = objectMapper.writeValueAsString(cartInfo);
			addcart(cartInfo);
		} catch (JsonProcessingException e) {  
			throw new RuntimeException("Failed to serialize CartInfo to JSON", e);  
		}  

		hashOps.put(cartKey, gid.toString(), cartInfoJson);  
	}
	
	

	// 删除购物车
	@Override
	public void removeFromCart(Integer uid, Integer gid) {  
		if (uid == null || gid == null) {  
			throw new IllegalArgumentException("User ID or Product ID cannot be null");  
		}  

		String cartKey = CART_PREFIX + uid;  

		HashOperations<String, String, String> hashOps = redisTemplate.opsForHash();  

		boolean isDeleted = hashOps.delete(cartKey, gid.toString()) != null;  

		if (!isDeleted) {  
			System.out.println("Item not found in cart or failed to remove");  
		} else {  
			System.out.println("Item successfully removed from cart");  
		}  
	}  

	// 查询购物车
	@Override
	public List<CartInfo> getCartItems(Integer uid) {
		List<CartInfo> cartItems = new ArrayList<>();  

		String cartKey = CART_PREFIX + uid;  

		HashOperations<String, String, String> hashOps = redisTemplate.opsForHash();  

		Map<String, String> cartMap = hashOps.entries(cartKey);  

		if (cartMap != null && !cartMap.isEmpty()) {  
			ObjectMapper objectMapper = new ObjectMapper();  
			for (Map.Entry<String, String> entry : cartMap.entrySet()) {  
				try {  
					CartInfo cartInfo = objectMapper.readValue(entry.getValue(), CartInfo.class);  
					cartInfo.setGid(Integer.parseInt(entry.getKey()));   
					cartItems.add(cartInfo);  
				} catch (Exception e) {  
					e.printStackTrace();  
				}  
			}  
		}  
		return cartItems;  
	}

	@Override
	public int addcart(CartInfo cf) {
		
		return cartInfoMapper.addcart(cf);
	}
}


